home *** CD-ROM | disk | FTP | other *** search
/ Shareware for Windows / Trust Shareware CD.iso / winshare / vb / frmvb1 / formatvb.wrk < prev    next >
Encoding:
Text File  |  1994-08-05  |  42.1 KB  |  867 lines

  1.      Controls     Sub CommandClear_Click...................149
  2.                   Sub CommandProcess_Click.................154
  3.                   Sub CommandQuit_Click....................186
  4.                   Sub Form_Load............................190
  5.                   Sub MenuDefaultPath_Click................205
  6.                   Sub MenuHelpAbout_Click..................231
  7.                   Sub MenuHelpIndex_Click..................247
  8.                   Sub MenuQuit_Click.......................250
  9.                   Sub TextBox_GotFocus.....................253
  10.  
  11.      Subroutines  Sub DeleteTemps..........................257
  12.                   Sub DoTextBox3...........................267
  13.                   Sub DumpSpecialCharacters................274
  14.                   Sub ExpandTabs...........................300
  15.                   Sub FormatVBHelp.........................346
  16.                   Sub GetFileRecords.......................351
  17.                   Sub GetInFileName........................420
  18.                   Sub GetOutFileName.......................475
  19.                   Sub GetPathFromIni.......................484
  20.                   Sub GetRandomRecSize.....................496
  21.                   Sub LoadBoxes............................514
  22.                   Sub PrintSepLine.........................557
  23.                   Sub PrintSub.............................560
  24.                   Sub PutFileRecords.......................579
  25.                   Sub PutTableOfContents...................670
  26.                   Sub SetColors............................710
  27.                   Sub SortEm...............................714
  28.                   Sub SortSwap.............................755
  29.                   Sub UpdateIni............................770
  30.                   Sub WriteEm..............................778
  31.                   Sub WriteJustSubAndFunRecords............790
  32.  
  33. '------------------------------------------------------------------------------'
  34.    1 VERSION 2.00
  35.    2 Begin Form FormFormatVB
  36.    3    Caption         =   "Format VB Program"
  37.    4    Height          =   4360
  38.    5    Icon            =   FORMATVB.FRX:0000
  39.    6    Left            =   1485
  40.    7    LinkMode        =   1  'Source
  41.    8    LinkTopic       =   "Form1"
  42.    9    ScaleHeight     =   3480
  43.   10    ScaleWidth      =   6705
  44.   11    Top             =   1520
  45.   12    Width           =   6855
  46.   13    Begin CommandButton CommandClear
  47.   14       Caption         =   "&Clear"
  48.   15       Height          =   620
  49.   16       Left            =   4560
  50.   17       TabIndex        =   5
  51.   18       Top             =   360
  52.   19       Width           =   855
  53.   20    End
  54.   21    Begin CommonDialog CMDialogFile
  55.   22       Left            =   240
  56.   23       Top             =   2640
  57.   24    End
  58.   25    Begin TextBox TextBox
  59.   26       Height          =   735
  60.   27       Index           =   1
  61.   28       Left            =   1800
  62.   29       MultiLine       =   -1  'True
  63.   30       TabIndex        =   1
  64.   31       Text            =   "Text1"
  65.   32       Top             =   1200
  66.   33       Width           =   4575
  67.   34    End
  68.   35    Begin CommandButton CommandQuit
  69.   36       Caption         =   "&Quit"
  70.   37       Height          =   620
  71.   38       Left            =   5520
  72.   39       TabIndex        =   3
  73.   40       Top             =   360
  74.   41       Width           =   855
  75.   42    End
  76.   43    Begin CommandButton CommandProcess
  77.   44       Caption         =   "&Process a Visual Basic File"
  78.   45       Height          =   620
  79.   46       Left            =   1800
  80.   47       TabIndex        =   0
  81.   48       Top             =   360
  82.   49       Width           =   2655
  83.   50    End
  84.   51    Begin PictureBox PictureIcon
  85.   52       AutoRedraw      =   -1  'True
  86.   53       AutoSize        =   -1  'True
  87.   54       BorderStyle     =   0  'None
  88.   55       Height          =   640
  89.   56       Left            =   240
  90.   57       Picture         =   FORMATVB.FRX:0302
  91.   58       ScaleHeight     =   640
  92.   59       ScaleWidth      =   480
  93.   60       TabIndex        =   4
  94.   61       Top             =   240
  95.   62       Width           =   480
  96.   63    End
  97.   64    Begin Label LabelBox
  98.   65       Alignment       =   1  'Right Justify
  99.   66       AutoSize        =   -1  'True
  100.   67       Caption         =   "LabelBox"
  101.   68       Height          =   195
  102.   69       Index           =   1
  103.   70       Left            =   840
  104.   71       TabIndex        =   2
  105.   72       Top             =   1200
  106.   73       Width           =   795
  107.   74    End
  108.   75    Begin Menu MenuOptions
  109.   76       Caption         =   "&Options"
  110.   77       Begin Menu MenuDefaultPath
  111.   78          Caption         =   "&Set Default Path"
  112.   79       End
  113.   80    End
  114.   81    Begin Menu MenuQuit
  115.   82       Caption         =   "&Quit"
  116.   83    End
  117.   84    Begin Menu MenuHelp
  118.   85       Caption         =   "&Help"
  119.   86       Begin Menu MenuHelpIndex
  120.   87          Caption         =   "&Index"
  121.   88          Shortcut        =   {F1}
  122.   89       End
  123.   90       Begin Menu MenuHelpSep
  124.   91          Caption         =   "-"
  125.   92       End
  126.   93       Begin Menu MenuHelpAbout
  127.   94          Caption         =   "&About"
  128.   95       End
  129.   96    End
  130.   97 End
  131. '------------------------------------------------------------------------------'
  132.   98 ' FormatVB.Frm - Format VB .txt file
  133.   99 ' 92/10/03 Copyright 1992, Larry Rebich, The Bridge, Inc.
  134.  100 ' 92/10/04 Add Table of Contents
  135.  101 ' 92/10/13 Use *.txt files
  136.  102 ' 92/10/16 Fix problems with Left Margin and Tabs Expansion
  137.  103 ' 92/12/01 Convert to VB 2.0, use .Frm files
  138.  104 ' 92/12/07 Add Captions to Table of Contents
  139.  105 ' 92/12/09 Add Help
  140.  106 ' 92/12/09 Send a copy to Inside Visual Basic, Cobb Group
  141.  108 DefInt A-Z                      'default data type is integer
  142.  109 Const Version = "1.0"                   'version
  143.  110 Const VersionDate = "December, 1992"    'version date, 92/12/09
  144.  111 Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String) As Integer
  145.  112 Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  146.  113 Const Pgm$ = "FormatVB Options" 'used in formatvb.ini file
  147.  114 Const Which$ = "Default Path"   'used in formatvb.ini as well
  148.  115 Const FileIni$ = "FormatVB.Ini" 'save default path name here, Windows Directory
  149.  116 Const FormatVBHelpFile = "FormatVB.Hlp" 'help file name
  150.  117 Const TempFName$ = "~ormatVB"   'temporary file name
  151.  118 Const MaxSubs = 1500    'maximum number of sub, functions - increase if necessary
  152.  119 Dim Recs$(1 To MaxSubs)         'store subs, functions here
  153.  120 Dim RecCount As Integer         'number of records read
  154.  121 Dim LFlag(1 To MaxSubs)         'flag, 0=normal text line
  155.  122 '                                      1=Sub, Control_ [contains underline]
  156.  123 '                                      3=Sub, Standard
  157.  124 '                                      5=End Sub
  158.  125 '                                      7=Function
  159.  126 '                                      8=End Function
  160.  127 Const xSub$ = "SUB"             'type 1 or 3
  161.  128 Const xFun$ = "FUNCTION"        'type 7
  162.  129 Const xEnd$ = "END"             'type 5 or 8
  163.  130 Dim SortRec$(1 To MaxSubs)      'put sub/function records here
  164.  131 Dim SortCt(1 To MaxSubs)        'and its record number
  165.  132 Dim SortInSubCount(1 To MaxSubs)'and sub/function contains this many
  166.  133 Dim SortLFlag(1 To MaxSubs)     'lflag here, type of sub/function
  167.  134 Dim SortThisMany As Integer     'how many subs and functions
  168.  135 Dim FirstSub As Integer         'first line containing sub or function
  169.  136 Dim InFile As String            'input file name
  170.  137 Dim OutFile As String           'output file
  171.  138 Dim RandomFile As String        'store them here randomly
  172.  139 Dim RandomRecSize As Integer    'random file record size
  173.  140 Dim LongestLen As Integer       'Longest line length
  174.  141 Dim LongestRec As Integer       'longest record number
  175.  142 Dim GotInFile As Integer        'we have a file switch
  176.  143 Dim PathName As String          'use this path name
  177.  144 Dim InCmDialog As Integer       'in here now switch
  178.  145 Dim VBFrmFile As Integer        'is it a VB 2.0 .frm file
  179.  146 Dim SepLine As String           'separator line
  180.  147 Dim AuthorIsUser As Integer     'is it the author
  181. '------------------------------------------------------------------------------'
  182.  149 Sub CommandClear_Click ()
  183.  150     For i = 1 To 3              'clear the text boxes
  184.  151         TextBox(i).Text = ""
  185.  152     Next
  186.  153 End Sub
  187. '------------------------------------------------------------------------------'
  188.  154 Sub CommandProcess_Click ()
  189.  155     Screen.MousePointer = HourGlass     'tell 'em to wait
  190.  156     CommandProcess.Enabled = False      'dim this control
  191.  157     CommandQuit.Enabled = False         'dim this one as well
  192.  158     CommandClear.Enabled = False
  193.  159     MenuOptions.Enabled = False         'dim this menu items
  194.  160     MenuQuit.Enabled = False
  195.  161     MenuHelp.Enabled = False
  196.  162     PictureIcon.SetFocus                'so no focus on text box
  197.  163     CommandClear_Click                  'clear the text boxes
  198.  164     GetInFileName                       'get the file to process
  199.  165     If GotInFile = False Then GoTo ExitThis 'open failed, cancel pressed
  200.  166     Screen.MousePointer = HourGlass     'back on in case set off in Dialog
  201.  167     GetOutFileName                      'get output file name from input
  202.  168     GetRandomRecSize                    'get largest line size
  203.  169     GetFileRecords                      'read the input file
  204.  170     WriteJustSubAndFunRecords           'write a temporary file
  205.  171     SortEm                              'sort the subroutine and function names
  206.  172     PutFileRecords                      'build the output file
  207.  173     Beep                                'tell 'em we are done
  208.  174     Screen.MousePointer = Default       'back to normal
  209.  175     DeleteTemps                         'delete temporary files
  210.  176 ExitThis:                               'if cancel pressed
  211.  177     CommandProcess.Enabled = True       'back on
  212.  178     CommandQuit.Enabled = True          'back on
  213.  179     CommandClear.Enabled = True
  214.  180     MenuOptions.Enabled = True
  215.  181     MenuQuit.Enabled = True
  216.  182     MenuHelp.Enabled = True
  217.  183     CommandProcess.SetFocus             'and light it
  218.  184     Screen.MousePointer = Default       'done waiting
  219.  185 End Sub
  220. '------------------------------------------------------------------------------'
  221.  186 Sub CommandQuit_Click ()
  222.  187     FormatVBHelp Help_Quit, 0&  'dump help file if active
  223.  188     End                         'quit
  224.  189 End Sub
  225. '------------------------------------------------------------------------------'
  226.  190 Sub Form_Load ()
  227.  191     CenterForm Me, 0, 0                 'center on screen
  228.  192     GetPathFromIni                      'get default path from ini
  229.  193     SetColors                           'some color is nice
  230.  194     LoadBoxes                           'set control locations
  231.  195     SepLine = "'" + String$(78, "-") + "'"  'separates subs and functions
  232.  196     RandomFile = TempFName + ".rnd"     'temp random file name
  233.  197     x$ = Environ$("AUTHOR")             'is author the user
  234.  198     If UCase$(x$) = UCase$("LarryRebich") Then
  235.  199         AuthorIsUser = True             'environ has author's name
  236.  200     End If
  237.  201     Show                                'show 'em
  238.  202     Refresh                             'force display before asking for file
  239.  203     CommandProcess_Click                'start 'em off with file dialog
  240.  204 End Sub
  241. '------------------------------------------------------------------------------'
  242.  205 Sub MenuDefaultPath_Click ()
  243.  206 'allow a default path name to be entered
  244.  207     P$ = "Enter a default path name, or press enter to retain the current path."
  245.  208     t$ = "Default Path"
  246.  209 TryAgain:
  247.  210     Value$ = InputBox$(P$, t$, PathName)
  248.  211     If Value$ = PathName Then Exit Sub  'no change
  249.  212     If Value$ = "" Then Exit Sub        'cancel pressed
  250.  213     If Right$(Value$, 1) <> "\" Then    'add ending \ if needed
  251.  214         Value$ = Value$ + "\"
  252.  215     End If
  253.  216     On Error GoTo BadDir                'if no file or bad name
  254.  217     x$ = Dir$(Value$ + "*.*")           'get any file
  255.  218     If x$ = "" Then                     'any file in directory?
  256.  219         Msg$ = "No files in directory: " + Value$
  257.  220         MsgBox Msg$, MB_IconExclamation, "Invalid Directory"
  258.  221         GoTo TryAgain
  259.  222     End If
  260.  223     PathName = Value$                   'store the new value
  261.  224     TextBox(1).Text = "  " + PathName   'into text box to display it
  262.  225     UpdateIni Value$                    'update the .ini file
  263.  226     Exit Sub
  264.  227 BadDir:
  265.  228     MsgBox Error$, MB_IconExclamation, "Failed to Find Any Files"
  266.  229     Resume TryAgain
  267.  230 End Sub
  268. '------------------------------------------------------------------------------'
  269.  231 Sub MenuHelpAbout_Click ()
  270.  232     Dim Msg As String, Nl As String * 2     'some info about the author
  271.  233     Dim Sp As String                        'some spaces
  272.  234     Sp = String$(9, " ")
  273.  235     Nl = Chr$(13) + Chr$(10)
  274.  236     Msg = "FormatVB - Format Visual Basic Text" + Nl
  275.  237     Msg = Msg + "Version: " + Version + " " + VersionDate + Nl + Nl
  276.  238     Msg = Msg + Sp + "Copyright " + Format$(Now, "yyyy") + Nl + Nl
  277.  239     Msg = Msg + Sp + "Larry Rebich" + Nl
  278.  240     Msg = Msg + Sp + "The Bridge, Inc." + Nl
  279.  241     Msg = Msg + Sp + "199 California Drive" + Nl
  280.  242     Msg = Msg + Sp + "Millbrae, CA  94030" + Nl + Nl
  281.  243     Msg = Msg + Sp + "415-697-2730" + Nl
  282.  244     Msg = Msg + Sp + "Fax: 415-692-3921"
  283.  245     MsgBox Msg, MB_IconQuestion, "About FormatVB"
  284.  246 End Sub
  285. '------------------------------------------------------------------------------'
  286.  247 Sub MenuHelpIndex_Click ()
  287.  248     FormatVBHelp Help_Context, 10&  'help requested
  288.  249 End Sub
  289. '------------------------------------------------------------------------------'
  290.  250 Sub MenuQuit_Click ()
  291.  251     CommandQuit_Click               'end it
  292.  252 End Sub
  293. '------------------------------------------------------------------------------'
  294.  253 Sub TextBox_GotFocus (Index As Integer)
  295.  254     If InCmDialog = True Then Exit Sub  'can't set focus while showing another screen
  296.  255     CommandProcess.SetFocus             'don't allow focus on the text boxes
  297.  256 End Sub
  298. '------------------------------------------------------------------------------'
  299.  257 Sub DeleteTemps ()
  300.  258     Dim Temp As String
  301.  259     Temp = TempFName + ".*"     'temp file names to delete
  302.  260 '    If AuthorIsUser Then        'for testing, delete the temps?
  303.  261 '        Msg$ = "Delete temporary files? "
  304.  262 '        MsgRtn% = MsgBox(Msg$, MB_YesNo + MB_IconQuestion, "Kill " + Temp)
  305.  263 '        If MsgRtn% = IDNo Then Exit Sub     'said no, so don't delete
  306.  264 '    End If
  307.  265     Kill PathName + Temp        'delete all temp files
  308.  266 End Sub
  309. '------------------------------------------------------------------------------'
  310.  267 Sub DoTextBox3 (RecCountPut As Integer, TheRec As String, Force As Integer)
  311.  268     Dim RecNum As String
  312.  269     If RecCountPut Mod 9 = 0 Or Force Then      'only every 9 or forced
  313.  270         RecNum = Format$(RecCountPut, "####")   'record number
  314.  271         TextBox(3).Text = RecNum + " " + TheRec 'now into text box
  315.  272     End If
  316.  273 End Sub
  317. '------------------------------------------------------------------------------'
  318.  274 Sub DumpSpecialCharacters (Rec As String)
  319.  275 'was needed for printer output, not needed for VB 2.0 Text Output
  320.  276     If VBFrmFile Then Exit Sub  'little faster
  321.  277     Dim Lf As String * 1        'line feed
  322.  278     Dim Cr As String * 1        'carriage return
  323.  279     Dim Ff As String * 1        'form feed
  324.  280     Dim x As String
  325.  281     Dim y As Integer
  326.  282     Lf = Chr$(10)
  327.  283     Cr = Chr$(13)
  328.  284     Ff = Chr$(12)
  329.  285     x = " " + Rec               'into x and add a blank
  330.  286     While InStr(x, Lf)          'dump line feeds
  331.  287         y = InStr(x, Lf)
  332.  288         x = Mid$(x, 1, y - 1) + Mid$(x, y + 1)
  333.  289     Wend
  334.  290     While InStr(x, Cr)          'dump carriage returns
  335.  291         y = InStr(x, Cr)
  336.  292         x = Mid$(x, 1, y - 1) + Mid$(x, y + 1)
  337.  293     Wend
  338.  294     While InStr(x, Ff)          'dump form feeds
  339.  295         y = InStr(x, Ff)
  340.  296         x = Mid$(x, 1, y - 1) + Mid$(x, y + 1)
  341.  297     Wend
  342.  298     Rec = Mid$(x, 2)            'dump blank that was added
  343.  299 End Sub
  344. '------------------------------------------------------------------------------'
  345.  300 Sub ExpandTabs (Rec As String)
  346.  301     Static Lm As Integer                'previous left margin
  347.  302     Dim SkipLmSet As Integer            'skip resetting setting left margin
  348.  303     Dim t As String * 1                 'tab
  349.  304     Dim s As String                     'spacer
  350.  305     Dim x As String                     'work string
  351.  306     Dim ExtraChars As String            'based upon left margin
  352.  307     ExtraChars = ""                     'clear for now
  353.  308     t = Chr$(9)                         'tab character
  354.  309     SkipLmSet = False                   'switch, off if any tab
  355.  310     If Lm > 1 Then                      'if margin greater than this
  356.  311         If InStr(Rec, t) > 0 Then       'and if there is a tab
  357.  312             ExtraChars = String$(Lm - 1, " ")   'add some more characters
  358.  313             SkipLmSet = True            'and skip setting left margin
  359.  314         End If
  360.  315     End If
  361.  316     If InStr(Rec, t) > 0 Then           'any tab
  362.  317         Rec = ExtraChars + Rec          'add the extra characters to the record
  363.  318     End If
  364.  319     x = " " + Rec                       'one blank
  365.  320     CountTabs = 0                       'count tabs
  366.  321     While InStr(x, t) > 0               'expand the tabs
  367.  322         CountTabs = CountTabs + 1       'double second tab
  368.  323         If CountTabs = 1 Then
  369.  324             s = String$(4, " ")
  370.  325         Else
  371.  326             s = String$(8, " ")
  372.  327         End If
  373.  328         i = InStr(x, t)
  374.  329         x = Mid$(x, 1, i - 1) + s + Mid$(x, i + 1)
  375.  330     Wend
  376.  331     Rec = Mid$(x, 2)                    'dump extra blank
  377.  332     If CountTabs > 4 Then               'should not get here!!
  378.  333         Msg$ = "Found " + Format$(CountTabs, "##0") + " tabs in line:" + Str$(RecCount)
  379.  334         Msg$ = Msg$ + "  Record: |" + Rec + "|.  The line may not expand correctly."
  380.  335         MsgBox Msg$, MB_IconExclamation, "Too Many Tabs?"
  381.  336     End If
  382.  337     If SkipLmSet = False Then
  383.  338         x = Mid$(x, 2)                  'and work variable
  384.  339         x = RTrim$(x)                   'get number of leading blanks
  385.  340         sl = Len(x)                     'length before dumping leading blanks
  386.  341         x = LTrim$(x)                   'dump leading blanks
  387.  342         el = Len(x)                     'length without leading blanks
  388.  343         Lm = sl - el + 1                'left margin
  389.  344     End If
  390.  345 End Sub
  391. '------------------------------------------------------------------------------'
  392.  346 Sub FormatVBHelp (WCmd%, dwData As Long)
  393.  347     Screen.MousePointer = HourGlass     'show 'em we are working
  394.  348     x% = WinHelp(hWnd, App.Path + FormatVBHelpFile, WCmd%, ByVal dwData)
  395.  349     Screen.MousePointer = Default       'done loading
  396.  350 End Sub
  397. '------------------------------------------------------------------------------'
  398.  351 Sub GetFileRecords ()               'read the records
  399.  352     ReDim a$(1 To 200)              'array for parse
  400.  353     Dim Rec As String               'read ascii file into here
  401.  354     Dim Blanks As String            'bunch of blanks
  402.  355     Blanks = String$(RandomRecSize, " ")    'fill random file with recs and blanks
  403.  356     Erase LFlag                     'zeros into this array
  404.  357     RecCount = 0                    'record counter
  405.  358     f = FreeFile                    'file id
  406.  359     Open PathName + InFile For Input As #f
  407.  360     f2 = FreeFile                   'next file id
  408.  361     Open PathName + RandomFile For Output As #f2 'work with a new one
  409.  362     Close #f2
  410.  363     Kill PathName + RandomFile      'dump the one just opened
  411.  364     Open PathName + RandomFile For Random As #f2 Len = RandomRecSize + 2
  412.  365     FirstSub = 0                    'this will contain the rec number of the first sub
  413.  366     While Not EOF(f)                'read until end of file
  414.  367         Line Input #f, Rec              'read the record
  415.  368         DumpSpecialCharacters Rec       'get rid of special characters
  416.  369         x$ = Trim$(Rec)                 'don't process completely blank records
  417.  370         If x$ <> "" Then
  418.  371             RecCount = RecCount + 1     'bump record counter
  419.  372             ExpandTabs Rec
  420.  373             Rec = Left$(Rec + Blanks, RandomRecSize)    'add blanks to pad record
  421.  374             Put #f2, RecCount, Rec          'store in random file
  422.  375             x$ = UCase$(LTrim$(Rec))        'work with it to see if Sub, End, etc.
  423.  376             If Left$(x$, 1) <> "'" Then     'if starts with comment then skip
  424.  377                 anum = Parse(x$, a$(), " ") 'split apart
  425.  378                 Select Case a$(1)           'first
  426.  379                     Case xSub$              'sub
  427.  380                         GoSub IfFirstSubFun 'is it the first one
  428.  381                         If InStr(a$(2), "_") > 0 Then
  429.  382                             LFlag(RecCount) = 1     'command_event
  430.  383                         Else
  431.  384                             LFlag(RecCount) = 3     'standard sub
  432.  385                         End If
  433.  386                         Recs(RecCount) = Rec        'store subs into matrix
  434.  387                         TextBox(2).Text = " " + Rec
  435.  388                     Case xFun$              'function
  436.  389                         GoSub IfFirstSubFun
  437.  390                         LFlag(RecCount) = 7
  438.  391                         Recs(RecCount) = Rec
  439.  392                         TextBox(2).Text = " " + Rec
  440.  393                     Case xEnd$
  441.  394                         Select Case a$(2)
  442.  395                             Case xSub$      'end sub
  443.  396                                 LFlag(RecCount) = 5
  444.  397                             Case xFun$      'end function
  445.  398                                 LFlag(RecCount) = 8
  446.  399                         End Select
  447.  400                     Case Else               'nothing special
  448.  401                         LFlag(RecCount) = 0
  449.  402                         DoTextBox3 RecCount, LTrim$(Rec), False
  450.  403                         Refresh
  451.  404                 End Select
  452.  405             Else
  453.  406                 DoTextBox3 RecCount, LTrim$(Rec), False
  454.  407             End If
  455.  408         End If
  456.  409     Wend
  457.  410     DoTextBox3 RecCount, LTrim$(Rec), True    'in case not shown
  458.  411     Close #f, #f2       'done with input and done creating random file
  459.  412     Reset
  460.  413     'in case there were no subs or functions [constant.txt!]
  461.  414     If FirstSub = 0 Then FirstSub = RecCount + 1
  462.  415     Exit Sub
  463.  416 IfFirstSubFun:
  464.  417     If FirstSub = 0 Then FirstSub = RecCount    'first sub record number
  465.  418     Return
  466.  419 End Sub
  467. '------------------------------------------------------------------------------'
  468.  420 Sub GetInFileName ()
  469.  421     Dim Fltr As String, f As Integer, Rec1 As String
  470.  422     InCmDialog = True                   'get file to process
  471.  423     CmDialogFile.DefaultExt = ".frm"    'default extension
  472.  424     CmDialogFile.DialogTitle = "VB Input File"
  473.  425     CmDialogFile.Filename = "*.frm"
  474.  426     Fltr = ""
  475.  427     Fltr = Fltr & "VB Forms [*.frm]|*.frm|"     'for VB 2.0 92/12/01
  476.  428     Fltr = Fltr & "Bas Files [*.bas]|*.bas|"
  477.  429     Fltr = Fltr & "Sub Files [*.sub]|*.sub|"
  478.  430     Fltr = Fltr & "Glb Files [*.glb]|*.glb|"
  479.  431     Fltr = Fltr & "Txt Files [*.txt]|*.txt|"
  480.  432     Fltr = Fltr & "Prn Files [*.prn]|*.prn|"
  481.  433     Fltr = Fltr & "All Files [*.*]|*.*|"
  482.  434     CmDialogFile.Filter = Fltr
  483.  435     CmDialogFile.Flags = OFN_READONLY Or OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST
  484.  436     CmDialogFile.CancelError = True     'allow cancel key to cause error
  485.  437     CmDialogFile.InitDir = PathName
  486.  438     GotInFile = True                    'say we got one
  487.  439     On Error Resume Next                'in case cancel pressed
  488.  440     CmDialogFile.Action = DLG_FILE_OPEN 'do it
  489.  441     If Err = 0 Then                     'ok, got name
  490.  442         InFile = CmDialogFile.Filetitle
  491.  443         PathName = CmDialogFile.Filename
  492.  444         PathName = Mid$(PathName, 1, InStr(PathName, InFile) - 1)
  493.  445         If Right$(PathName, 1) <> "\" Then
  494.  446             PathName = PathName + "\"
  495.  447         End If
  496.  448         If InStr(LCase$(InFile), ".frm") > 0 Then 'VB 2.0
  497.  449             f = FreeFile                'check to see if it is valid
  498.  450             Open PathName & InFile For Input As #f
  499.  451             Line Input #f, Rec1
  500.  452             Rec1 = Trim$(Rec1)
  501.  453             If Rec1 <> "VERSION 2.00" Then
  502.  454                 GotInFile = False
  503.  455                 Beep
  504.  456                 TextBox(1).Text = "  " + LCase$(PathName + InFile) + " - Not a VB 2.0 File"
  505.  457             Else
  506.  458                 GoSub PathAndIniUpdate
  507.  459                 VBFrmFile = True        'it is a VB .frm file
  508.  460             End If
  509.  461         Else                            'not VB 2.0
  510.  462             GoSub PathAndIniUpdate
  511.  463             VBFrmFile = False           'not VB .frm file
  512.  464         End If
  513.  465     ElseIf Err = 32755 Then             'cancel pressed
  514.  466         GotInFile = False
  515.  467     End If
  516.  468     InCmDialog = False                  'done with this process
  517.  469     Exit Sub
  518.  470 PathAndIniUpdate:                       'ok, store it
  519.  471     UpdateIni PathName                  'store it
  520.  472     TextBox(1).Text = "  " + LCase$(PathName + InFile) + " - Input"
  521.  473     Return
  522.  474 End Sub
  523. '------------------------------------------------------------------------------'
  524.  475 Sub GetOutFileName ()                   'get OutFile from InFile
  525.  476     Dim x As String
  526.  477     If InStr(InFile, ".") > 0 Then      'find period
  527.  478         x = Mid$(InFile, 1, InStr(InFile, ".") - 1)
  528.  479         OutFile = x + ".wrk"
  529.  480     Else
  530.  481         OutFile = "FormatVB.Wrk"        'should not get here
  531.  482     End If
  532.  483 End Sub
  533. '------------------------------------------------------------------------------'
  534.  484 Sub GetPathFromIni ()
  535.  485     Dim Buf As Integer, Value As String, Num As Integer
  536.  486     Buf = 64                            'read the .ini file
  537.  487     Value = Space$(Buf)
  538.  488     Num = GetPrivateProfileString(Pgm$, Which, "", Value, Buf, FileIni)
  539.  489     If Num > 0 Then
  540.  490         PathName = Trim$(Mid$(Value, 1, Num))
  541.  491     Else
  542.  492         PathName = ""                   'no .ini value found
  543.  493     End If
  544.  494     TextBox(1).Text = "  " + PathName   'display it
  545.  495 End Sub
  546. '------------------------------------------------------------------------------'
  547.  496 Sub GetRandomRecSize ()
  548.  497 'get the longest line, needed to set the random record length
  549.  498     Dim x As String
  550.  499     Dim z As Integer
  551.  500     f = FreeFile                            'get the largest line number
  552.  501     Open PathName + InFile For Input As #f
  553.  502     While Not EOF(f)
  554.  503         Line Input #f, x                    'read line
  555.  504         DumpSpecialCharacters x             'drop special characters
  556.  505         ExpandTabs x                        'expand it
  557.  506         x = RTrim$(x)                       'dump any trailing blanks
  558.  507         z = Len(x)                          'size of remaining record
  559.  508         If RandomRecSize < z Then           'if x longer then use save it
  560.  509             RandomRecSize = z               'use it
  561.  510         End If
  562.  511     Wend
  563.  512     Close #f                                'done, close it
  564.  513 End Sub
  565. '------------------------------------------------------------------------------'
  566.  514 Sub LoadBoxes ()
  567.  515     Static HereBefore As Integer        'set up the screen
  568.  516     Of = 100
  569.  517     If HereBefore = False Then          'do this just once
  570.  518         HereBefore = True
  571.  519         For i = 2 To 3
  572.  520             Load LabelBox(i)            'load the extra labels and boxes
  573.  521             LabelBox(i).Visible = True
  574.  522             Load TextBox(i)
  575.  523             TextBox(i).Visible = True
  576.  524         Next
  577.  525         For i = 1 To 3
  578.  526             If i < 3 Then
  579.  527                 TextBox(i).Height = CommandProcess.Height * .75
  580.  528             Else
  581.  529                 TextBox(i).Height = CommandProcess.Height * 1.5
  582.  530                 TBLeft = TextBox(i).Width * .35
  583.  531                 TextBox(i).Width = TextBox(1).Width + TBLeft
  584.  532             End If
  585.  533         Next
  586.  534     End If
  587.  535     TextBox(1).Top = CommandProcess.Top + CommandProcess.Height + Of * 2
  588.  536     TextBox(1).Left = CommandProcess.Left
  589.  537     TextBox(2).Left = TextBox(1).Left
  590.  538     TextBox(3).Left = TextBox(1).Left - TBLeft
  591.  539     TextBox(2).Top = TextBox(1).Top + TextBox(1).Height + Of
  592.  540     TextBox(3).Top = TextBox(2).Top + TextBox(1).Height + Of
  593.  541     LabelBox(1).Caption = "File"
  594.  542     LabelBox(2).Caption = "Routine"
  595.  543     LabelBox(3).Caption = "Line"
  596.  544     For i = 1 To 3
  597.  545         If i > 1 Then
  598.  546             TextBox(i).Text = ""
  599.  547         End If
  600.  548         LabelBox(i).Top = TextBox(i).Top + Of
  601.  549         LabelBox(i).Left = TextBox(i).Left - LabelBox(i).Width - Of * 2
  602.  550         If i = 3 Then
  603.  551             LabelBox(i).Left = LabelBox(i).Left - TBLeft
  604.  552         End If
  605.  553         LabelBox(i).BackColor = BackColor
  606.  554         LabelBox(i).ForeColor = ForeColor
  607.  555     Next
  608.  556 End Sub
  609. '------------------------------------------------------------------------------'
  610.  557 Sub PrintSepLine (f As Integer)
  611.  558     PrintSub f, SepLine, 0      'print a separator line
  612.  559 End Sub
  613. '------------------------------------------------------------------------------'
  614.  560 Sub PrintSub (f As Integer, PLine As String, LineNumber As Integer)
  615.  561 ' common print subroutine
  616.  562     Static HoldLine As String
  617.  563     If HoldLine = SepLine And PLine = SepLine Then  'no two sep together
  618.  564         Exit Sub
  619.  565     End If
  620.  566     HoldLine = PLine
  621.  567     Dim Counter As String
  622.  568     If LineNumber > 0 Then              'print line number, unless zero
  623.  569         Counter = Right$("    " + Format$(LineNumber, "####"), 4)
  624.  570         Print #f, Counter; " "; PLine
  625.  571         If Len(PLine) > LongestLen Then
  626.  572             LongestRec = LineNumber     'new value
  627.  573             LongestLen = Len(PLine)     'and save for compare
  628.  574         End If
  629.  575     Else
  630.  576         Print #f, PLine     'don't count this line, usually a separator
  631.  577     End If
  632.  578 End Sub
  633. '------------------------------------------------------------------------------'
  634.  579 Sub PutFileRecords ()
  635.  580 ' write them to the .wrk file now, almost done
  636.  581     Dim HaveBeginSw As Integer, HaveEndSw As Integer
  637.  582     LongestRec = 0                      'reset this
  638.  583     LongestLen = 0                      'and this
  639.  584     RecCountPut = 0                     'record counter
  640.  585     CommentStringLen = 40
  641.  586     CommentString$ = String$(CommentStringLen, "'")
  642.  587     TextBox(1).Text = " " + LCase$(PathName + OutFile) + " - Output"
  643.  588     f = FreeFile
  644.  589     Open PathName + OutFile For Output As #f
  645.  590     f2 = FreeFile                       'random file
  646.  591     Open PathName + RandomFile For Random As #f2 Len = RandomRecSize + 2
  647.  592     If SortThisMany >= 1 Then
  648.  593         PutTableOfContents f, f2        'do the table of contents
  649.  594         PrintSepLine f
  650.  595     End If
  651.  596     If FirstSub > 1 Then                'any general
  652.  597         For j = 1 To FirstSub - 1       'general info
  653.  598             GoSub WriteRec
  654.  599             DoTextBox3 RecCountPut, x$, False
  655.  600         Next
  656.  601         PrintSepLine f
  657.  602     End If
  658.  603     For i = 1 To SortThisMany           'do Command_Click type first
  659.  604         If SortLFlag(i) = 1 Then
  660.  605             For j = SortCt(i) To SortCt(i) + SortInSubCount(i) - 1
  661.  606                 GoSub WriteRec
  662.  607                 GoSub IntoTextBox
  663.  608             Next
  664.  609             PrintSepLine f
  665.  610         End If
  666.  611     Next
  667.  612     For i = 1 To SortThisMany           'do normal subs next
  668.  613         If SortLFlag(i) = 3 Then
  669.  614             For j = SortCt(i) To SortCt(i) + SortInSubCount(i) - 1
  670.  615                 GoSub WriteRec
  671.  616                 GoSub IntoTextBox
  672.  617             Next
  673.  618             PrintSepLine f
  674.  619         End If
  675.  620     Next
  676.  621     For i = 1 To SortThisMany           'do functions next
  677.  622         If SortLFlag(i) = 7 Then
  678.  623             For j = SortCt(i) To SortCt(i) + SortInSubCount(i) - 1
  679.  624                 GoSub WriteRec
  680.  625                 GoSub IntoTextBox
  681.  626             Next
  682.  627             PrintSepLine f
  683.  628         End If
  684.  629     Next
  685.  630     'wrap up
  686.  631     x$ = String$(4, " ")
  687.  632     m$ = "####"
  688.  633     PrintSub f, Right$(x$ + Format$(RecCountPut, m$), 4) + " lines in file " + LCase$(PathName + OutFile), 0
  689.  634     PrintSub f, Right$(x$ + Format$(LongestLen, m$), 4) + " characters in longest line", 0
  690.  635     PrintSub f, Right$(x$ + Format$(LongestRec, m$), 4) + " first longest line", 0
  691.  636     Close                           'close any open files
  692.  637     Reset                           'force buffers to disk
  693.  638     Exit Sub
  694.  639 WriteRec:                           'write the temp file
  695.  640     RecCountPut = RecCountPut + 1
  696.  641     Get #f2, j, x$
  697.  642     x$ = RTrim$(x$)
  698.  643     y$ = LTrim$(x$)                 'dump long strings with only '''''
  699.  644     If Left$(y$, CommentStringLen) <> CommentString$ Then
  700.  645         PrintSub f, x$, RecCountPut
  701.  646         If VBFrmFile Then               'is this a VB 2.0 form
  702.  647             If HaveEndSw = False Then   'only do this once
  703.  648                 If HaveBeginSw = False Then
  704.  649                     If Left$(LCase$(x$), 5) = "begin" Then
  705.  650                         HaveBeginSw = True
  706.  651                     End If
  707.  652                 Else
  708.  653                     If Left$(LCase$(x$), 3) = "end" Then
  709.  654                         HaveEndSw = True
  710.  655                         PrintSepLine f  'separator after last end
  711.  656                     End If
  712.  657                 End If
  713.  658             End If
  714.  659         End If
  715.  660     End If
  716.  661     Return
  717.  662 IntoTextBox:                        'show record in text box
  718.  663     If j = SortCt(i) Then           'sub or function name
  719.  664         TextBox(2).Text = " " + x$
  720.  665     Else                            'just an ordinary record
  721.  666         DoTextBox3 RecCountPut, LTrim$(x$), True
  722.  667     End If
  723.  668     Return
  724.  669 End Sub
  725. '------------------------------------------------------------------------------'
  726.  670 Sub PutTableOfContents (f As Integer, f2 As Integer)
  727.  671 'write the table of contents to the .wrk file
  728.  672     Dim Toc As String                   'sub into here
  729.  673     Dim LToc As String                  'local
  730.  674     Dim HoldFlag As Integer             'extra line on type break
  731.  675     ReDim SecType(1 To 7) As String     'section names stored here
  732.  676     Dim SecLen As Integer               'store section len here
  733.  677     SecType(1) = "     Controls    "    'section headings
  734.  678     SecType(3) = "     Subroutines "
  735.  679     SecType(7) = "     Functions   "
  736.  680     SecLen = Len(SecType(1))            'longest one
  737.  681     f9 = FreeFile                       'work file
  738.  682     TocOffset = SecLen                  'TOC offset
  739.  683     ReDim AToc(1 To 500) As String
  740.  684     Open PathName + TempFName + ".toc" For Output As #f9
  741.  685     StartLine = FirstSub                'first subroutine line number
  742.  686     For i = 1 To SortThisMany           'this many to put in Toc
  743.  687         Get #f2, SortCt(i), Toc         'get the sub
  744.  688         Toc = LTrim$(RTrim$(Toc))
  745.  689         aTocNum = Parse(Toc, AToc(), " ")'just sub and name
  746.  690         LToc = Left$(AToc(1) + " " + AToc(2) + String$(40, "."), 40)
  747.  691         LToc = String$(TocOffset, " ") + LToc + Right$("....." + Format$(StartLine, "####"), 4)
  748.  692         LToc = SecType(SortLFlag(i)) + Mid$(LToc, TocOffset)    'add caption
  749.  693         SecType(SortLFlag(i)) = String$(SecLen, " ")'kill it after first one
  750.  694         If i > 1 Then                   'not first time
  751.  695             If HoldFlag <> SortLFlag(i) Then    'extra line on Flag break
  752.  696                 HoldFlag = SortLFlag(i)
  753.  697                 Print #f9, ""           'blank line between types
  754.  698                 Print #f, ""
  755.  699             End If
  756.  700         Else
  757.  701             HoldFlag = SortLFlag(i)     'first time, set hold flag
  758.  702         End If
  759.  703         Print #f9, LToc                 'work file
  760.  704         Print #f, LToc                  'real file
  761.  705         StartLine = StartLine + SortInSubCount(i)
  762.  706     Next
  763.  707     Print #f, ""                        'extra line after TOC
  764.  708     Close #f9                           'close temp file
  765.  709 End Sub
  766. '------------------------------------------------------------------------------'
  767.  710 Sub SetColors ()
  768.  711     BackColor = Application_Workspace   'some color is nice
  769.  712     ForeColor = Window_Text
  770.  713 End Sub
  771. '------------------------------------------------------------------------------'
  772.  714 Sub SortEm ()
  773.  715     Erase SortInSubCount            'clear the arrays
  774.  716     Erase SortRec
  775.  717     Erase SortCt
  776.  718     SortThisMany = 0
  777.  719     For i = 1 To RecCount
  778.  720         Select Case LFlag(i)        'build sort array
  779.  721             Case 1, 3, 7            'sub or function
  780.  722                 SortThisMany = SortThisMany + 1
  781.  723                 ReDim RecArray$(1 To 500)
  782.  724                 x$ = Recs(i)            'into unindexed string
  783.  725                 RecArrayNumber = Parse(x$, RecArray$(), " ")
  784.  726                 x$ = RecArray$(1) + " " + RecArray$(2)
  785.  727                 SortRec(SortThisMany) = x$  'the sub, function
  786.  728                 SortCt(SortThisMany) = i    'record number
  787.  729                 SortLFlag(SortThisMany) = LFlag(i)
  788.  730                 SortInSubCount(SortThisMany) = SortInSubCount(SortThisMany) + 1
  789.  731             Case Else               'all other types
  790.  732                 If SortThisMany > 0 Then    'count records in sub or function
  791.  733                     SortInSubCount(SortThisMany) = SortInSubCount(SortThisMany) + 1
  792.  734                 End If
  793.  735         End Select
  794.  736     Next
  795.  737     WriteEm PathName + TempFName + ".nrt"   'write unsorted temp file for debug
  796.  738     For i = 1 To SortThisMany - 1   'sort decending by name, end up ascending
  797.  739         For j = i + 1 To SortThisMany
  798.  740             If SortRec(i) < SortRec(j) Then     'swap them
  799.  741                 SortSwap i, j
  800.  742             End If
  801.  743         Next
  802.  744     Next
  803.  745     WriteEm PathName + TempFName + ".srt"   'write sort by name for debug
  804.  746     For i = 1 To SortThisMany - 1   'sort by type, end up ascending
  805.  747         For j = i + 1 To SortThisMany
  806.  748             If SortLFlag(i) >= SortLFlag(j) Then 'swap them
  807.  749                 SortSwap i, j
  808.  750             End If
  809.  751         Next
  810.  752     Next
  811.  753     WriteEm PathName + TempFName + ".typ"   'write final sort for debug
  812.  754 End Sub
  813. '------------------------------------------------------------------------------'
  814.  755 Sub SortSwap (i As Integer, j As Integer)
  815.  756     Dim Tmp As String, TmpCt As Integer
  816.  757     Tmp = SortRec(i)                'swap sort array elements
  817.  758     SortRec(i) = SortRec(j)
  818.  759     SortRec(j) = Tmp
  819.  760     TmpCt = SortCt(i)
  820.  761     SortCt(i) = SortCt(j)
  821.  762     SortCt(j) = TmpCt
  822.  763     TmpCt = SortInSubCount(i)
  823.  764     SortInSubCount(i) = SortInSubCount(j)
  824.  765     SortInSubCount(j) = TmpCt
  825.  766     TmpCt = SortLFlag(i)
  826.  767     SortLFlag(i) = SortLFlag(j)
  827.  768     SortLFlag(j) = TmpCt
  828.  769 End Sub
  829. '------------------------------------------------------------------------------'
  830.  770 Sub UpdateIni (Value As String)
  831.  771     Dim Result As Integer           'update the .ini file
  832.  772     Result = WritePrivateProfileString(Pgm$, Which$, LCase$(Value$), FileIni$)
  833.  773     If Result = 0 Then              'should not get an error
  834.  774         Msg$ = "Could not update " + UCase$(Which$) + "=" + UCase$(Value$) + " in File: " + UCase$(FileIni$)
  835.  775         MsgBox Msg$, MB_IconExclamation, "Update INI Error"
  836.  776     End If
  837.  777 End Sub
  838. '------------------------------------------------------------------------------'
  839.  778 Sub WriteEm (WFile As String)
  840.  779     Dim x1 As String, x2 As String, x3 As String
  841.  780     f = FreeFile                'write temporary files, for debug
  842.  781     Open WFile For Output As #f
  843.  782     For i = 1 To SortThisMany
  844.  783         x1 = Right$("    " + Trim$(Str$(SortCt(i))), 4)  'starting number
  845.  784         x2 = Right$("    " + Trim$(Str$(SortInSubCount(i))), 4)  'records in sub
  846.  785         x3 = Right$(" " + Trim$(Str$(SortLFlag(i))), 1)   'type
  847.  786         Print #f, x1; " "; x2; " "; x3; " "; SortRec(i)
  848.  787     Next
  849.  788     Close #f
  850.  789 End Sub
  851. '------------------------------------------------------------------------------'
  852.  790 Sub WriteJustSubAndFunRecords ()
  853.  791     f = FreeFile                        'temporary file
  854.  792     Open PathName + TempFName + ".lst" For Output As #f
  855.  793     For i = 1 To RecCount
  856.  794         If Len(Recs(i)) > 0 Then
  857.  795             Print #f, FirstSub; " "; LFlag(i); " "; Recs(i)
  858.  796         End If
  859.  797     Next
  860.  798     Close #f
  861.  799     Reset
  862.  800 End Sub
  863. '------------------------------------------------------------------------------'
  864.  800 lines in file g:\user\cdproj\sharew\vb\formatvb\formatvb.wrk
  865.  240 characters in longest line
  866.  112 first longest line
  867.